/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.lib;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author tiepvv
 */
public class Utils {

    public static List<Long> listInt2Long(List<Integer> ids) {
        if (ids == null) {
            return null;
        }
        List<Long> lisLong = new ArrayList<Long>();

        for (Iterator<Integer> it = ids.iterator(); it.hasNext();) {
            lisLong.add(Long.valueOf(it.next()));
        }
        return lisLong;
    }

    public static String getCookieByName(HttpServletRequest req, String cookieName) {
        Cookie[] cookies = req.getCookies();
        String value = null;
        for (int i = 0; i < cookies.length; i++) {
            String name = cookies[i].getName();
            if (name.equals(cookieName)) {
                value = cookies[i].getValue();
            }
        }
        return value;
    }

    public static String getUserAgent(HttpServletRequest req) {
        String userAgent = req.getHeader("User-Agent");
        if (userAgent == null) {
            userAgent = "";
        }
        return userAgent;
    }

    public static void redirect(HttpServletResponse resp, String url) {
        resp.setStatus(HttpServletResponse.SC_FOUND);
        resp.setHeader("Location", url);
    }
}
